//
//  ViewController.m
//  05-UIScrollView
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
// 宏定义 命名两种方式
// 宏嵌套
// 宏就是系统编译时， 代码的替换（预编译指令）
#define kScreenBounds [UIScreen mainScreen].bounds
 // 一， 全大写
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
 //二，k ＋ 驼峰命名
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController () <UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIScrollView *scrollerView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    scrollerView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:scrollerView];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 10, kScreenWidth - 20, 100);
    button.backgroundColor = [UIColor redColor];
    [scrollerView addSubview:button];
   // 1. 默认CGSizeZero CGSizeMake（0 ， 0）
   // contentSize 设置可以滚动的范围
    scrollerView.contentSize = CGSizeMake(kScreenWidth * 4, 1000);
   // 2. 默认 CGPointZero
   // contentOffset 偏移量， 滚动的位置
   // 不设置contentSize，contentOffset也可以改变
   // 问题 ： 改变 contentOffset 什么变化了？
   // 答案 ： scrollView 的bounds 有了变化
    NSLog(@"scrollView frame is : %@", NSStringFromCGRect(scrollerView.frame));
    NSLog(@"scrollView bounds is : %@", NSStringFromCGRect(scrollerView.bounds));
    NSLog(@"scrollView center is : %@", NSStringFromCGPoint(scrollerView.center));
    scrollerView.contentOffset = CGPointMake(0, 500);
    
   // 3.contentInset 添加滚动区域四周的滚动范围
    scrollerView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
   //4. directionalLockEnabled 锁定垂直或水平滚动 （45’角， 锁会失效）
    scrollerView.directionalLockEnabled = YES;
   //5.bounces 回弹效果
//    scrollerView.bounces = NO;
    //6.alwaysBounceVertical 在没有垂直方向的滚动范围时， 保证垂直方向上的回弹效果存在
    scrollerView.alwaysBounceVertical = YES;
   //7.alwaysBounceHorizontal  在没有水平方向的滚动范围时， 保证水平方向上的回弹效果存在
    scrollerView.alwaysBounceHorizontal = YES;
   //8. pagingEnabled 按页滚动
    scrollerView.pagingEnabled = YES;
   //9. scrollEnabled 滚动试图能否滚动， 默认YES（可以滚）， NO（不可以滚）
    scrollerView.scrollEnabled = YES;
   // 10.是否显示垂直／水平的滚动指示器， 默认YES
    scrollerView.showsHorizontalScrollIndicator = YES;
    scrollerView.showsVerticalScrollIndicator = YES;
  //11. 滚动指示器的边距
    scrollerView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
  //12.indicatorStyle 滚动指示器的颜色（深色背景，浅色背景）
    scrollerView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
  //   13.
 //tracking 用户是否开始拖动
//    scrollerView.tracking
  //dragging 用户是否拖动中
//scrollerView.dragging
  //decelerating 是否减速中
//  scrollerView.decelerating
 //14. 滚动试图 双击状态栏手势是否存在 默认YES
    scrollerView.scrollsToTop = NO;
  // 15.scrollView的 代理
    scrollerView.delegate = self;
  //16 键盘消失模式 (UIScrollViewKeyboardDismissModeOnDrag 手指滑动时取消键盘)
    scrollerView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
  //测试
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 300, kScreenWidth - 20, 200)];
    textField.backgroundColor = [UIColor whiteColor];
    [scrollerView addSubview:textField];
    
  //17.缩放系数
   //如果想进行试图缩放， 必须实现viewForZoomingInScrollView：
    scrollerView.minimumZoomScale = 0.5;
    scrollerView.maximumZoomScale = 2;
}
// 与其相要缩放的子试图绑定， scrollView的contentSize会跟随这个子试图变化
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}

// 滚动试图滚动， 就会执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  //tracking 用户触摸
    if (scrollView.isTracking) {
         NSLog(@"正在拖动滚动");
    }
  //Dragging 用户开始滑动
    if (scrollView.isDragging) {
        
    }
  //decelerating 用户触摸结束
    if (scrollView.decelerating) {
        
    }
}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始拖拽");
}

//已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"结束拖拽");
}

//将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    NSLog(@"开始减速");
}
//已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        NSLog(@"结束减速");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
